package com.hust.bridgecheck.controller.Settings;

import com.hust.bridgecheck.entity.*;
import com.hust.bridgecheck.service.BridgeSensor.BridgeService;
import com.hust.bridgecheck.service.BridgeSensor.SensorService;
import com.hust.bridgecheck.service.Settings.NotifyService;
import com.hust.bridgecheck.service.Settings.ThresholdService;
import com.hust.bridgecheck.service.Settings.WarningSService;
import com.hust.bridgecheck.service.UserManagement.DomainService;
import com.hust.bridgecheck.service.UserManagement.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("setting")
public class SettingPageController {
    //setting的一些操作
    @Autowired
    ThresholdService thresholdService;

    @Autowired
    DomainService domainService;

    @Autowired
    GroupService groupService;

    @Autowired
    WarningSService warningSService;

    @Autowired
    NotifyService notifyService;

    @Autowired
    BridgeService bridgeService;

    @Autowired
    SensorService sensorService;
    @GetMapping("/threshold")
    public String viewThreshold(Model model){
        List<Object> t_lists = thresholdService.getAllDetailThreshold();
        List<Domain> d_lists = domainService.getAllDomain();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String current = df.format(new Date());
         model.addAttribute("t_lists",t_lists);
         model.addAttribute("d_lists",d_lists);
         model.addAttribute("current",current);
        return "yuzhiedit";
    }
    @GetMapping("/warning")
    public String viewWarning(Model model){
        //获取所有组的名称
        List<UserGroup> g_list = groupService.getAllGroup();
        model.addAttribute("g_list",g_list);
        return "baojingsetting";
    }

    @GetMapping("/notify")
    public String viewNotify(Model model){
        //获取所有的通知
        List<Notify> n_lists = notifyService.getAllNotify();
        List<UserGroup> g_list = groupService.getAllGroup();
        model.addAttribute("g_list",g_list);
        model.addAttribute("n_lists",n_lists);
        return "tongzhi";
    }

    @PostMapping("/addNotify")
    public String addNotify(Notify notify){
        //提交一些通知
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String Current = df.format(new Date());// new Date()为获取当前系统时间
        notify.setReaded(0);
        notify.setCreateTime(Current);
        notifyService.addNotify(notify);
        return "redirect:/setting/notify";
    }

    @PostMapping("/addThreshold")
    private String addThreshold(Threshold threshold){
        Threshold addedthreshold = thresholdService.addThreshold(threshold);
        return "redirect:/setting/threshold";
    }

    @PostMapping("/addWarningSetting")
    private String addWarningSetting(@RequestParam("ThresholdNum") Integer thresholdNum, @RequestParam("reportMethod") Integer reportMethod, @RequestParam("reportID") Integer[] reportID){
        //java对数组进行循环遍历
        String StrID = "";
        for(int i=0; i<reportID.length;i++)
        {
            StrID += reportID[i];
            if(i != reportID.length - 1 ){
                StrID += ";";
            }
        }
        WarningSetting warningSetting = new WarningSetting(thresholdNum,reportMethod,StrID);
        warningSService.addWarningSetting(warningSetting);
        return "redirect:/setting/warning";
    }

    @GetMapping("/viewReport/{RID}")
    private String viewReport(Model model,@PathVariable("RID") Integer RID){
        //更新数据库表示已经读了
        Notify notifyDetail = notifyService.getNotify(RID);
        model.addAttribute("notifyDetail",notifyDetail);
        notifyService.UpdateReaded(RID);
        return "viewreport";
    }

    @GetMapping("/SearchThreshold/{name}")
    private String SearchThreshold(Model model,@PathVariable("name") String name){
        Object t_lists = thresholdService.getThresholdByThresholdName(name);
        List<Domain> d_lists = domainService.getAllDomain();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String current = df.format(new Date());
        model.addAttribute("t_lists",t_lists);
        model.addAttribute("d_lists",d_lists);
        model.addAttribute("current",current);
        return "yuzhiedit";
    }
}
